package com.wing.productionPlan.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wing.common.enums.MaintenanceEnum;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.productionPlan.model.entity.ProductionWorkOrder;
import com.wing.productionPlan.model.request.ProductionWorkOrderForm;
import com.wing.productionPlan.model.response.DeviceOrWorkVo;
import com.wing.productionPlan.model.response.ProductionWorkOrderVO;
import com.wing.productionPlan.model.response.ProductionWorkStationVO;
import com.wing.productionPlan.service.ProductionProcessService;
import com.wing.productionPlan.service.ProductionWorkOrderService;
import com.wing.web.annotation.RequestObjectFieldIgnore;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 工站生产任务-Controller
 *
 * @author xiayang
 */
@Slf4j
@Validated
@Api(value = "工位生产任务管理", tags = "#/productionPlan/productionWorkStation")
@RestController("productionWorkStationController")
@RequestMapping("productionPlan/admin/productionWorkStation")
@RequiredArgsConstructor
public class ProductionWorkStationController {

    private final ProductionWorkOrderService productionWorkOrderService;
    private final ProductionProcessService productionProcessService;

    @ApiOperation(value = "查询工位生产任务列表", notes = "搜索")
    @GetMapping("/list")
    public PageResult<ProductionWorkStationVO> list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit,
            @RequestParam(defaultValue = "create_time") String sort,
            @RequestParam(defaultValue = "asc") String order
            ) {
        IPage<ProductionWorkStationVO> pageList = productionWorkOrderService
                .getProductionWorkStationVOPageList(page, limit);
        List<ProductionWorkStationVO> records = pageList.getRecords();

        List<String> collect = records.stream().map(ProductionWorkStationVO::getGzsbId).collect(Collectors.toList());

        List<DeviceOrWorkVo> fessionlist = productionProcessService.fessionlist(collect);
        List<DeviceOrWorkVo> devicelist = productionProcessService.devicelist(collect);

        //获取记录
        if (CollectionUtil.isNotEmpty(records)) {
            for (ProductionWorkStationVO record : records) {
                record.setWorkstationNatureName(MaintenanceEnum.workstationEmum.getNameByCode(record.getWorkstationNature()));
                if (MaintenanceEnum.workstationEmum.SBGW.getCode().equals(record.getWorkstationNature())) {
                    if (CollectionUtil.isNotEmpty(devicelist)) {
                        for (DeviceOrWorkVo deviceOrWorkVo : devicelist) {
                            if (deviceOrWorkVo.getId().toString().equals(record.getGzsbId())) {
                                record.setGzsbName(deviceOrWorkVo.getDeviceName());
                            }
                        }
                    }
                }else{
                    if (CollectionUtil.isNotEmpty(fessionlist)) {
                        for (DeviceOrWorkVo deviceOrWorkVo : fessionlist) {
                            if (deviceOrWorkVo.getId().toString().equals(record.getGzsbId())) {
                                record.setGzsbName(deviceOrWorkVo.getDeviceName());
                            }
                        }
                    }
                }
            }
        }
        return new PageResult<>(pageList.getRecords(), pageList.getTotal());
    }

}
